home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 13 - 1997 (partial) / 13.03 Mar 97 / Challenge Solution, Tangrams / Tangrams.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-12-08  |  5.8 KB  |  207 lines  |  [TEXT/R*ch]

  1. /* Tangrams.h    */
  2.  
  3. #pragma mark Problem Specification
  4.  
  5. typedef    struct MyVertex {
  6.     float    h;    /* horizontal coordinate of vertex    */
  7.     float    v;    /* vertical coordinate of vertex    */
  8. }    MyVertex;
  9.  
  10. #define    kMaxVertices 10
  11.  
  12. typedef struct MyPolygon {
  13.     long        numVertices;    /*the number of vertices*/
  14.     MyVertex    vertex[kMaxVertices];    /*the vertices*/
  15. }    MyPolygon;
  16.  
  17. typedef struct MyTransform {
  18.     float    flipH;    /* If doFlip - flip polygon about this horiz.coord.*/
  19.     float    rotateClockwise;    /* radians to rotate - */
  20.     float    rotateCenterH;        /* - around this horiz coord - */
  21.     float    rotateCenterV;        /* - and this vert coord */
  22.     float    translateH;        /* translate this far horiz */
  23.     float    translateV;        /* translate this far vert */
  24.     Boolean    doFlip;    /* Flip if true */
  25. }    MyTransform;
  26.  
  27. void SolveTangram(
  28.     MyPolygon    *theShape,    /* shape to be assembled */
  29.     long        numHoles,    /* number of holes in theShape (>=0) */
  30.     MyPolygon    *theHoles[],    /* holes in theShape (>=0) */
  31.     long        numPieces,    /* number of pieces in theShape */
  32.     MyPolygon    *thePieces[],    /* pieces in theShape */
  33.     MyTransform    *theXForms[]    /* transform for each piece */
  34. );
  35.  
  36. /* INCLUDES */
  37.  
  38. #include <math.h>
  39.  
  40. #define MyError(msg)  ExitToShell();
  41.  
  42. /* CLASSES */
  43.  
  44. class TVector
  45. {
  46. /*    A vector expressed in polar coordinates */
  47.     friend class TList;
  48.     protected:
  49.         float    angle;    // clockwise radians
  50.         float    length;    // length
  51.         TVector    *next;
  52.     public:
  53.     //    Constructors
  54.         TVector();
  55.         TVector(TVector& v);
  56.         TVector(float dir, float len);
  57.     //    Operators
  58.         TVector& operator = (TVector& aVector);
  59.         TVector& operator += (TVector& aVector);
  60.         TVector& operator -= (TVector& aVector);
  61.         Boolean operator == (TVector& aVector);
  62.         Boolean operator > (TVector& aVector);
  63.         Boolean operator < (TVector& aVector);
  64.         Boolean operator || (TVector& aVector); /* parallel */
  65.     //    Access
  66.         float    getLength ();
  67.         float    getAngle ();
  68.         TVector* getNext ();
  69.         void    set (float dir, float len);
  70.         void    setLength (float len);
  71.         void    setAngle (float dir);
  72.         void    setNext (TVector *aVector);
  73.     //    Transformation
  74.         void    rotate (float anAngle);
  75.         void    reverse ();
  76.         void    flip (float around);
  77.     //    Cartesian/Polar conversion
  78.         void    setHV (float h, float v);
  79.         void    setHV (MyVertex *pt);
  80.         void    getHV (MyVertex *pt);
  81.     //    Is Intercept of the line from p1 to p2 on h=0 < distance?
  82.         friend    Boolean Intercept (TVector *p1start, TVector *p1,
  83.                                     TVector *p2start, TVector *p2);
  84. };
  85.  
  86. class TList
  87. {
  88. /*    A list of TVector instances */
  89.     protected:
  90.         TVector    *start;
  91.         TVector    *end;
  92.     public:
  93.         TList();
  94.         ~TList();
  95.         void    IList(TList& aList);
  96.         void    insert (TVector *aVector); /* add aVector to the end */
  97.         void    insert (TVector *aVector, TVector *afterVector);
  98.         void    remove (TVector *aVector);
  99.         TVector*    first (void); /* return first node */
  100.         TVector*    next (TVector *aVector);
  101.         TVector*    last (void); /* return last node */
  102.         Boolean    valid (TVector *aVector);
  103. };
  104.  
  105. class TPolygon : public TVector
  106. {
  107. /*    A Polygon can be rotated and translated.
  108.     "Align" places the polygon with its first side along the first side of the
  109. input polygon
  110.     "Rotate" turns the polygon through an angle about its origin
  111.     "Translate" moves the polygon along a vector */
  112.     protected:
  113.         TList    sides;
  114.     /*    A list of Vectors defining the direction and distance
  115.         of each vertex from its predecessor.
  116.         The Vector in the base class stores direction and
  117.         distance from the origin to the start point. */
  118.         
  119.     public:
  120.     /*    Constructors */
  121.         TPolygon ();
  122.         TPolygon (TPolygon& p);
  123.         void    IPolygon(TPolygon *shape);
  124.         void    IPolygon(MyPolygon *shape);
  125.     /*    Access */
  126.         TVector    getOrigin (void);
  127.         TVector*    firstSide (void);
  128.         TVector*    nextSide (TVector *aSide);
  129.         TVector*    lastSide (void);
  130.         float    getRotation (void);
  131.     /*    Transformations */
  132.         void    rotate (float angle);
  133.         void    rotateTo (float direction);
  134.         void    translate (TVector *where);
  135.         void    translateTo (TVector *where);
  136.         
  137.         Boolean    fill (TList *unplaced, TList *placed);
  138.         Boolean    contains (TPolygon *p);
  139.         void    align (TPolygon *thePiece);
  140.         void    subtract (TPolygon *aPiece);
  141. };
  142.  
  143. class TShape : public TPolygon
  144. {
  145. /*    A Shape is a Polygon used to fill a Tangram.
  146.     It may be flipped and turned.
  147.     It remembers its first defined side and flipped state
  148.     "Flip" turns the shape over about the perpendicular
  149.         bisector of its current first side
  150.     "Turn" moves the shape so that its next side and vertex take
  151.         the start point and direction of its current first side and vertex */
  152.     protected:
  153.         Boolean    flipped;
  154.     /*    Is this Shape currently flipped? */
  155.         TVector*    firstDefinedSide;
  156.     /*    Stores a pointer to the first side */
  157.     public:
  158.         TShape();
  159.         void    IShape (MyPolygon *theData);
  160.         virtual    Boolean canFlip(void) {return false;}
  161.         /*    The base class cannot be flipped and always returns false */
  162.         Boolean    flip(void);
  163.         /*    Flip the Shape if canFlip()
  164.             Return false if it is not now flipped. */
  165.         Boolean    turn ();
  166.         /*    Change the current first side.
  167.             Return false if the next side is the firstDefinedSide */
  168.         virtual    void getTransform (MyTransform *theTransform[]);
  169. };
  170.  
  171. class TPiece : public TShape
  172. {
  173. /*    A Piece is a Shape that can really be flipped, and
  174.     It remembers the index of the input data that defined it,
  175.     and the direction and relative position of the first defined side for recovery
  176.     of its transform into the MyTransform structure at that index */
  177.     private:
  178.         long    index;
  179.         TVector    firstOrigin;
  180.         TVector    firstDirection;
  181.     public:
  182.         void    IPiece (MyPolygon *theData[], long i);
  183.         virtual    Boolean canFlip(void) {return true;}
  184.         virtual    void getTransform (MyTransform *theTransform[]);
  185. };
  186.  
  187. class TTangram : public TPolygon
  188. {
  189. /*    The Tangram to solve is a Polygon that we want to fill with Shapes */
  190.     protected:
  191.         TList    unplaced;
  192.         TList    placed;
  193.         MyTransform    **xForms;
  194.  
  195.     public:
  196.         TTangram ();
  197.         TTangram (TTangram& aShape);
  198.         void    ITangram (MyPolygon *theShape,
  199.                         long numHoles, MyPolygon *theHoles[],
  200.                         long numPieces, MyPolygon *thePieces[],
  201.                         MyTransform    *xForms[]);
  202.         void    solve(void);
  203. };
  204.  
  205.  
  206.  
  207.